#include "StdAfx.h"
#include "matrix.h"
#include "ustringutils.h"
#include "MatlabStrategyRunner.h"
USING_LIBSPACE

bool CMatlabStrategyRunner::LoadStrategy( TCHAR* strategyname )
{
	m_strategyname = strategyname;
	tstring m_strategydllname = _getStrategyDllName(m_strategyname);

	if(!UFileSystem::IsFileExist(m_strategydllname.c_str()) ) 
	{
		DBG_TRACE((m_strategydllname + _T(" not existed")));
		return false;
	}


	m_hdll = UDll::OpenDll(m_strategyname.c_str());
	if(m_hdll == NULL)
	{

		return false;
	}

	UDll::GetDllAllExportFunc(m_strategydllname.c_str(),&m_exportsfuncnames);

	std::string initf = _getInitFuncName(m_strategyname);

	UDll::PFUNC* func = UDll::GetDllFunc(m_hdll,initf.c_str());
	func();



	RegisterEvent();
	return true;
}

typedef  bool FuncDef(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[]);
int CMatlabStrategyRunner::svc()
{
	std::string funcname =_getMainFuncName(m_strategyname);
	UDll::PFUNC*  pfunc = UDll::GetDllFunc(m_hdll,funcname.c_str());
	if(pfunc != NULL)
	{
		try
		{

			pfunc();
		}

		catch (...)
		{
		}

		return 0;
	}
	else
	{
		//	SetLas
	}

	return -1;
}

tstring CMatlabStrategyRunner::_getStrategyDllName( const tstring& name )
{
	return name + _T(".dll");
}

CMatlabStrategyRunner::~CMatlabStrategyRunner(  )
{
	if( UDll::IsValidDll(m_hdll))
	{
		UDll::CloseDll(m_hdll);
		m_hdll = UDll::inValidDll;

	}
}

std::string CMatlabStrategyRunner::_getRegisterEventFuncName( const tstring& name )
{

	tstring exportname =  name + _T("_event");
	return _getMatchedFuncName(exportname);	
}

BOOL CMatlabStrategyRunner::RegisterEvent()
{
	std::string eventname = _getRegisterEventFuncName(m_strategyname);


	UDll::PFUNC* func = UDll::GetDllFunc(m_hdll,eventname.c_str());	


	if(func != NULL)
	{
		func();
		return TRUE;
	}	
	return FALSE;
}

std::string CMatlabStrategyRunner::_getInitFuncName( const tstring& name )
{
	return _getMatchedFuncName(name + _T("Initialize") );
}

std::string CMatlabStrategyRunner::_getTerminateFuncName( const tstring& name )
{
	return _getMatchedFuncName( name +_T("Terminate") );
}

BOOL CMatlabStrategyRunner::HasEventDefinition()
{
	std::string eventname = _getRegisterEventFuncName(m_strategyname);	
	UDll::PFUNC* func = UDll::GetDllFunc(m_hdll,eventname.c_str());	
	return func != NULL;
}

tstring CMatlabStrategyRunner::GetName() const
{
	return m_strategyname;
}

CMatlabStrategyRunner::CMatlabStrategyRunner()
{

}

std::string CMatlabStrategyRunner::_getMatchedFuncName( tstring exportname )
{
	
	for (std::vector<std::string>::size_type i=0; i< m_exportsfuncnames.size(); ++i)
	{
		tstring name = UStrConvUtils::ToUpper( UStrConvUtils::ToTS(m_exportsfuncnames[i]) );
		
		if(UStrConvUtils::ToUpper(exportname) == UStrConvUtils::ToUpper(name))
			return m_exportsfuncnames[i];
	}
	return "";
}

std::string CMatlabStrategyRunner::_getMainFuncName( const tstring& name )
{
	tstring funcname = tstring(_T("mlf") ) + m_strategyname;
	return  _getMatchedFuncName(funcname);
}
